/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui;

import net.worlds.ui.UI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import net.worlds.WorldsException;
import net.worlds.object.GameObject;


public class PartsTreeSelectionListener implements TreeSelectionListener{

    public void valueChanged(TreeSelectionEvent e) {
        for(TreePath p : e.getPaths())
        {
            if(!(p.getLastPathComponent() instanceof PartTreeNode))
                throw new RuntimeException("illegal tree node type");
            else if(e.isAddedPath(p))
            {
                PartTreeNode n = (PartTreeNode) p.getLastPathComponent();
                GameObject part = n.getObject();
                UI.select(part);
            }
            else if(!e.isAddedPath(p))
            {
                PartTreeNode n = (PartTreeNode) p.getLastPathComponent();
                GameObject part = n.getObject();
                UI.deselect(part);
            }
        }
        try {
            UI.updateSelection();
        } catch (WorldsException ex) {
            throw new RuntimeException(ex);
        }
    }

}
